package com.example.xiaosuan.recyclerviewtest_304fjl;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.xiaosuan.recyclerviewtest_304fjl.iface.ArticleListener;
import com.example.xiaosuan.recyclerviewtest_304fjl.model.ArticleModel;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private MyAdapter myAdapter;
    private List<ArticleBean> list=null;

ArticleListener articleListener=new ArticleListener() {
    @Override
    public void onResponse(List<ArticleBean> beenlist) {
 list=beenlist;//得到网络上的数据
        myAdapter.setList(list);//传给adapter
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
        myAdapter.notifyDataSetChanged();
    }
};
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                initRecyclerView();


    ArticleModel articleModel=new ArticleModel();
    articleModel.getResultList("article",2,"rv1l2tidbomann2qn86nln6ur2",articleListener);
//shilihua

}

                private List<ArticleBean> getData(){
                List<ArticleBean> list = new ArrayList<ArticleBean>();
                for (int i = 1; i <= 6; i++) {
                    ArticleBean art;
                    art = new ArticleBean();
                  //  art.setArticleId(i);
                  //  art.setTitle("标题"+i);
                  //  art.setImgid(R.drawable.f1);
                  //  art.setTime("2017-4-6");
            art.setDescription("描述"+i);
            list.add(art);
        }
        return list;
    }

    private void initRecyclerView() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        //创建默认的线性LayoutManager
        layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true);
        //创建并设置Adapter
        list = getData();
        myAdapter = new MyAdapter(this);
        myAdapter.setList(list);
        recyclerView.setAdapter(myAdapter);
    }
}
